<template>
  <div class="comment-reply">
    <van-nav-bar
    :title="comment.reply_count>0?`${comment.reply_count}条回复`:'暂无回复'"
    fixed placeholder
    >
      <template #left>
        <van-icon name="cross" @click="$emit('close')" />
      </template>
    </van-nav-bar>

    <!-- 当前评论项 -->
    <CommentItem :comment="comment"></CommentItem>
    <!-- /当前评论项 -->

    <!-- 评论回复 -->
    <van-cell title="全部回复"></van-cell>
    <CommentList
    :source="comment.com_id"
    :type="'c'"
    :list="commentList"
    @onload-success='onloadSuccess'
    ></CommentList>
    <!-- /评论回复 -->

    <!-- 发布评论按钮-->
    <div class="post-wrap">
      <van-button round size="small" class="post-btn" @click="show=true">写回复</van-button>
    </div>
    <!-- /发布评论按钮 -->

    <van-popup v-model:show="show" position="bottom">
    <CommentPost
    :target='comment.com_id'
    :articleId='articleId'
    @post-success="onPostSuccess"
    ></CommentPost>
    </van-popup>
  </div>
</template>

<script>
import { ref, inject } from 'vue'
import CommentItem from './comment-item.vue'
import CommentList from './comment-list.vue'
import CommentPost from './comment-post.vue'
export default {
  name: 'CommentReply',
  components: {
    CommentItem,
    CommentList,
    CommentPost
  },
  props: {
    comment: {
      type: Object,
      required: true
    }
  },
  setup (props, context) {
    const articleId = inject('articleId')// 接收祖辈组件provide传过来的数据
    const show = ref(false)
    const commentList = ref([])// 腾转挪移之评论回复列表
    // 渲染评论回复
    function onloadSuccess (data) {
      const { results } = data
      commentList.value.push(...results)// 评论回复列表
    }
    // 发布评论回复
    function onPostSuccess (data) {
      show.value = false
      commentList.value.unshift(data.new_obj)
      context.emit('onPostSuccess')
    }
    return {
      onloadSuccess,
      onPostSuccess,
      commentList,
      show,
      articleId
    }
  }
}
</script>

<style lang='less' scoped>
.post-wrap{
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 88px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  border-top: 1px solid #d8d8d8;
  .post-btn{
    width: 60%;
  }
}
</style>
